#include "BTree.h"
#include "BufferManager.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	if(argc != 3)
	{
		cout << "Usage:" << endl;
		cout << "\t" << "./test pagenum buffertype" << endl;
		return -1;
	}
	int pagesize = 128;
	int pagenum = atoi(argv[1]);
	int width = 100;
	int hisize = 10000;
	int buffertype = atoi(argv[2]);
	BufferManager * bm = new BufferManager(500000, pagenum, pagesize, width, hisize, buffertype);
	BTree * btree = new BTree(pagesize, bm);

	ifstream fin;
	fin.open("zipf_data");
	int count;
	int value;
	fin >> count;
	vector<int> data;
	for(int i = 0; i < count; i++)
	{
		fin >> value;
		data.push_back(value);
	}

	cout <<"Total number of keys: "<< count << endl;

	for(int i = 0; i < count; i++)
	{
		//cout << i << endl;
		btree->InsertKey(data[i]);
	}

	//int sum = 0; 

	/*
	for(int i = 0; i < count; i++)
	{
		if(data[i] != btree->PointQuery(data[i]))
		{
			sum++;
			cout << i << endl;
		}
	}
	

	cout << "Error: " << sum << endl;
	cout << "Read IO: " << btree->bm->GetReadIO() << endl;
	cout << "Write IO: " << btree->bm->GetWriteIO() << endl;
	cout << "Total buffer: " << pagenum  << endl;
	cout << "Total disk: " << btree->bm->GetCurrentID() << endl;
	*/

	ofstream fout;
	fout.open("result.txt", ios::out | ios::app);
	fout << "PageNum:" << pagenum << " BufferType:" << buffertype << " ReadIO:" << btree->bm->GetReadIO()
		<< " WriteIO:" << btree->bm->GetWriteIO() << endl;
	fout.close();

	return 0;
}	
